<?php
declare (strict_types=1);
namespace app\user\model\user;

use app\admin\model\site\PersonalGroup;
use app\common\logic\Attach;
use app\common\model\BaseModel;
use think\model\concern\SoftDelete;

/**
 * 定义个人中心列表数据模型
 */
class Personal extends BaseModel
{
    use SoftDelete;
    /**
     * 表名
     *
     * @var string
     */
    protected $name = 'site_personal_config';

    /**
     * 定义表主键
     *
     * @var string
     */
    protected $pk = 'id';

    // 定义全局的查询范围
    protected $globalScope = ['open'];
    //
    // public function scopeStatus($query)
    // {
    //     $query->where('status', 1);
    // }

    public function scopeOpen($query)
    {
        $query->where('open', 1);
    }

    public function getIconAttr($value): string
    {
        $attach = Attach::find($value);
        $re     = $attach ? $attach->fileurl : '';

        return is_array($re) ? '' : $re;
    }

    public function groupTitle()
    {
        return $this->belongsTo(PersonalGroup::class, 'group_id')->bind([
            'group_title' => 'title',
            'group_key'   => 'key',
            'group_sort'  => 'sort'
        ]);
    }

    public function personalData()
    {
        return $this->hasOne(PersonalData::class, 'config_id', 'id')
            ->with(['iconUrl'])
            ->bind(['icon_url', 'data_title' => 'title', 'status']);
    }
}
